//
//  A_ViewController.swift
//  Test
//
//  Created by Louis Liu on 26/01/2018.
//  Copyright © 2018 Louis Liu. All rights reserved.
//

import UIKit
import JLRoutes
import SFFundation
//import SVRecord
import AVFoundation

class A_ViewController: UIViewController {

    @IBOutlet weak var speekText: UITextField!
    @IBOutlet weak var textfield: UITextField!
    @IBOutlet weak var soundType: UISegmentedControl!
    @IBOutlet weak var speekRate: UISlider!
    @IBOutlet weak var rateLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func onTapBackground(_ sender: Any) {
        self.view.endEditing(true)
    }
    
    @IBAction func pushToC(_ sender: Any) {
        let url = ComponentRouter.UserHomepage.url
        JLRoutes.global().routeURL(url, withParameters: ["userID":559127])
    }
    
    @IBAction func showLoginView(_ sender: Any) {
//        JLRoutes.global().routeURL(ComponentRouter.LoginView.url, withParameters: nil)
//        let videoRecorder:SVPhotoClipView = UIStoryboard.viewFromSVRecord()
//        videoRecorder.rawImage = UIImage(named: "wudi-ding-.jpg")
//        self.present(videoRecorder, animated: true, completion: nil)
    }
    
    @IBAction func onTapShowTrackDetail(_ sender: Any) {
        let param = ["trackID":"\(textfield.text!)"]
        JLRoutes.global().routeURL(ComponentRouter.ShowTrackDetail.url, withParameters: param)
    }
    
    @IBAction func onTapCircle(_ sender: Any) {
        JLRoutes.global().routeURL(ComponentRouter.CircleIndexView.url, withParameters: nil)
    }
    
    let synth = AVSpeechSynthesizer() //TTS对象
    let audioSession = AVAudioSession.sharedInstance() //语音引擎

    @IBAction func saySomething(_ sender: Any) {
        synth.stopSpeaking(at: AVSpeechBoundary.immediate)
        var voice = ""
        
        switch soundType.selectedSegmentIndex {
        case 0:
            voice = "com.apple.ttsbundle.siri_male_zh-CN_compact"
        case 1:
            voice = "com.apple.ttsbundle.siri_female_zh-CN_compact"
        case 2:
            voice = "com.apple.ttsbundle.Ting-Ting-compact"
        default:
            voice = "com.apple.ttsbundle.siri_female_zh-CN_compact"
        }
        try! audioSession.setCategory(AVAudioSessionCategoryPlayback)
        
        let utterance = AVSpeechUtterance(string:speekText.text!)
        utterance.voice = AVSpeechSynthesisVoice(identifier: voice)
        utterance.volume = 1
//        utterance.preUtteranceDelay = 0.5
//        utterance.postUtteranceDelay = 0.5
        utterance.rate = speekRate.value
        synth.speak(utterance)
    }
    
    @IBAction func rateChange(_ sender: Any) {
        rateLabel.text = "\(speekRate.value)"
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
     }

}
